package cn.wolfcode.ssm.web.controller;

import cn.wolfcode.ssm.domain.Hero;
import cn.wolfcode.ssm.query.HeroQueryObject;
import cn.wolfcode.ssm.query.PageResult;
import cn.wolfcode.ssm.service.impl.HeroServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class HeroController {
    @Autowired
    private HeroServiceImpl heroService;


    @RequestMapping("/list")
    public String list(Model model,HeroQueryObject queryObject){
        model.addAttribute("qo",queryObject);
        PageResult pageResult = heroService.queryForList(queryObject);
        model.addAttribute("pageResult",pageResult);
        return "list";
    }


    @RequestMapping("/listc")
    public String list_b(Model model){
        List<Hero> heroes = heroService.selectAll();
        model.addAttribute("heroes",heroes);
        return "list";
    }
    @RequestMapping("/delete")
    public String delete(Long id ){
        heroService.delete(id);
        return "redirect:list";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null){
            Hero hero = heroService.selectOne(id);
            model.addAttribute("hero",hero);
            return "/input";
        }else{
            return "/input";
        }
    }
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Hero hero){
        if (hero.getId() != null){
            hero.setName("肥佬叫鸡被警察抓了");

            heroService.update(hero);
            return "redirect:list";
        }else {
            heroService.insert(hero);
            return "redirect:list";
        }
    }
}
